package com.mx.spring.security.handler;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.mx.spring.dev.exception.MxException;
import com.mx.spring.dev.result.MxResult;
import com.mx.spring.security.base.bean.LoginResult;
import com.mx.spring.security.base.model.SecurityUser;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: mengxiang.
 * @create: 2021-10-27 15:03
 * @Description:
 */
public interface ILoginHandler {

    /**
     * 登录之前
     *
     * @param params
     * @return
     * @throws MxException
     */
    MxResult<LoginResult> loginBefore(Map<String, String> params) throws MxException;

    /**
     * 登录成功
     *
     * @param params
     * @param securityUser
     * @return
     * @throws MxException
     */
    MxResult<LoginResult> loginSuccess(Map<String, String> params, SecurityUser securityUser) throws MxException;

    /**
     * 登录失败
     *
     * @param params
     * @param securityUser
     * @return
     * @throws MxException
     */
    MxResult<LoginResult> loginFail(Map<String, String> params, SecurityUser securityUser) throws MxException;


    /**
     * 登录完成事件
     *
     * @param params
     * @param securityUser
     * @param saTokenInfo
     * @return
     * @throws MxException
     */
    void loginComplete(Map<String, String> params, SecurityUser securityUser, SaTokenInfo saTokenInfo) throws MxException;

    /**
     * 退出之前
     *
     * @param params
     * @return
     * @throws MxException
     */
    MxResult<LoginResult> logoutBefore(Map<String, String> params) throws MxException;

    /**
     * 退出之后
     *
     * @param params
     * @return
     * @throws MxException
     */
    MxResult<LoginResult> logoutAfter(Map<String, String> params) throws MxException;

    /**
     * 自定义检查登录逻辑 默认不处理 验证失败抛出notLogin异常
     *
     * @param securityUser
     * @throws MxException
     */
    void checkLoginCustom(SecurityUser securityUser) throws MxException;


    @Component
    class DefaultLoginHandler implements ILoginHandler {

        @Override
        public MxResult<LoginResult> loginBefore(Map<String, String> params) throws MxException {
            return MxResult.ok();
        }

        @Override
        public MxResult<LoginResult> loginSuccess(Map<String, String> params, SecurityUser securityUser) throws MxException {
            return MxResult.ok();
        }

        @Override
        public MxResult<LoginResult> loginFail(Map<String, String> params, SecurityUser securityUser) throws MxException {
            return MxResult.ok();
        }

        @Override
        public void loginComplete(Map<String, String> params, SecurityUser securityUser, SaTokenInfo saTokenInfo) throws MxException {
        }

        @Override
        public MxResult<LoginResult> logoutBefore(Map<String, String> params) throws MxException {
            return MxResult.ok();
        }

        @Override
        public MxResult<LoginResult> logoutAfter(Map<String, String> params) throws MxException {
            return MxResult.ok();
        }

        @Override
        public void checkLoginCustom(SecurityUser securityUser) throws MxException {

        }
    }
}
